package cn.blogger.controller;

import cn.blogger.constant.MessageConstant;
import cn.blogger.entity.PageResult;
import cn.blogger.entity.Result;
import cn.blogger.pojo.TUser;
import cn.blogger.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

/**
 * by:lin 2021/11/22 10:17
 */
@RestController
@CrossOrigin
@RequestMapping("/user")

public class UserController {
    @Reference
    private UserService userService;
    //增加用户
    @PostMapping(value="/add.do")
//    public Result add(@RequestParam(value = "username") String username,
//                      @RequestParam(value = "password") String password,
//                      @RequestParam(value = "gender") String gender,
//                      @RequestParam(value = "avatar") @Nullable String avatar,
//                      @RequestParam(value = "email") String email,
//                      @RequestParam(value = "birthday") String birthday,
//                      @RequestParam(value = "mobile") String mobile,
//                      @RequestParam(value = "qqNumber") @Nullable String qqNumber,
//                      @RequestParam(value = "weChat") @Nullable String weChat){
    public Result add(@RequestBody TUser user){
//        TUser user= new TUser(username,password,gender,avatar,email,birthday,mobile,new Date().toString(),qqNumber,weChat);
        try {
            userService.addUser(user);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
    //查询全部用户
    @GetMapping(value="/all.do")
    public PageResult all(@RequestParam(value = "currentPage", defaultValue = 1 + "") String sCurrentPage,
                          @RequestParam(value = "pageSize", defaultValue = 10 + "") String sPageSize,
                          @RequestParam("queryString") @Nullable String queryString) {
        int currentPage, pageSize;
        try {
            currentPage = Integer.parseInt(sCurrentPage);
            pageSize = Integer.parseInt(sPageSize);
        } catch (Exception ignored) {
            currentPage = 1;
            pageSize = 10;
        }
        return  userService.selectAllUser(currentPage, pageSize, queryString);
    }
    //根据用户id查询用户
    @DeleteMapping(value="/selectById.do")
    public Result byId(@RequestParam int uid){
        TUser user = new TUser();
        try {
            user = (TUser) userService.selectUserById(uid);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL,user);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS,user);
    }
    //根据用户id删除用户
    @DeleteMapping(value="/remove.do")
    public Result remove(@RequestParam int uid){
        try {
            userService.removeUser(uid);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
    //修改用户
    @PutMapping(value="/update.do")
    public Result update(@RequestBody TUser user){
        try {
            userService.updateUser(user);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
}
